﻿using ControlzEx.Standard;
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfMyText.VM;
using static MaterialDesignThemes.Wpf.Theme;

namespace WpfMyText
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isResizing = false;
        private Point startPoint;
        private double startWidth;
        private double startHeight;
        private double startLeft;
        private double startTop;
        List<Window> windows = new List<Window>();
        public MainWindowVM mainWindowVM;
        public MainWindow()
        {
            mainWindowVM = new MainWindowVM();
            InitializeComponent();
           
            DataContext = mainWindowVM;

            //inputText.DataContext = mainWindowVM;
            inputText.ContextMenu = null;
            filePathMenuItems.ItemsSource = mainWindowVM.MenuItems;
           
        }
        

       

        private async void inputText_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (e.Key == Key.W)
                {
                    bool visual = await mainWindowVM.LetAIAppendText();
                     if (visual)
                    {
                        AITextControl.Visibility = Visibility.Visible;
                    }
                   
                }
                else if (e.Key == Key.S)
                {
                    // 按下 Alt + S 时执行的命令
                    mainWindowVM.AppendAIResponse();
                }
            }

        }


        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        
        private bool isMouseWheelDown = false;

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.MiddleButton == MouseButtonState.Pressed)
            {
                isMouseWheelDown = true;
                this.CaptureMouse();
                mouseDownPoint = e.GetPosition(this);
            }
        }

        private Point mouseDownPoint;

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseWheelDown && this.IsMouseCaptured)
            {
                Point currentPoint = e.GetPosition(this);
                double offsetX = currentPoint.X - mouseDownPoint.X;
                double offsetY = currentPoint.Y - mouseDownPoint.Y;

                this.Left += offsetX;
                this.Top += offsetY;
            }
        }
        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (isMouseWheelDown && this.IsMouseCaptured)
            {
                isMouseWheelDown = false;
                this.ReleaseMouseCapture();
            }
           

        }

       

        private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ContextMenu1.IsOpen = true;
        }


        private void exitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

     

        private void openNewFileMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (mainWindowVM.StartCheck())
            {
               MessageBox.Show("打开新文件成功！");
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            UserSettingWindow userSettingWindow = new UserSettingWindow();
            windows.Add(userSettingWindow);
            userSettingWindow.Show();
        }

        private void inputText_TextChanged(object sender, TextChangedEventArgs e)
        {
            mainWindowVM.WriteToFile();
        }

        //private void inputText_MouseDown(object sender, MouseButtonEventArgs e)
        //{
        //    Window_MouseDown(sender, e);
        //}

        private void inputText_MouseMove(object sender, MouseEventArgs e)
        {
            Window_MouseMove(sender, e);
        }

        private void inputText_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Window_MouseUp(sender, e);
        }

        private void inputText_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Window_MouseDown(sender, e);
        }

        private void window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            foreach (var window in windows)
            {
                window.Close();
            }
        }
        #region 暂缓
        // 左侧调整区域鼠标按下事件处理
        private void LeftResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.Left);
        }

        // 右侧调整区域鼠标按下事件处理
        private void RightResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.Right);
        }

        // 顶部调整区域鼠标按下事件处理
        private void TopResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.Top);
        }

        // 底部调整区域鼠标按下事件处理
        private void BottomResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.Bottom);
        }

        // 左上角调整区域鼠标按下事件处理
        private void TopLeftResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.TopLeft);
        }

        // 右上角调整区域鼠标按下事件处理
        private void TopRightResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.TopRight);
        }

        // 左下角调整区域鼠标按下事件处理
        private void BottomLeftResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.BottomLeft);
        }

        // 右下角调整区域鼠标按下事件处理
        private void BottomRightResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StartResizing(e, ResizeDirection.BottomRight);
        }

        // 鼠标移动事件处理
        private void ResizeBorder_MouseMove(object sender, MouseEventArgs e, ResizeDirection direction)
        {
            if (isResizing )
            {
                Point currentPoint = e.GetPosition(null);
                double deltaX = currentPoint.X - startPoint.X;
                double deltaY = currentPoint.Y - startPoint.Y;

                
                switch (direction)
                {
                    case ResizeDirection.Left:
                        Width = startWidth - deltaX;
                        Left = startLeft + deltaX;
                        break;
                    case ResizeDirection.Right:
                        Width = startWidth + deltaX;
                        
                        break;
                    case ResizeDirection.Top:
                        Height = startHeight - deltaY;
                        Top = startTop + deltaY;
                        break;
                    case ResizeDirection.Bottom:
                        Height = startHeight + deltaY;
                        break;
                    case ResizeDirection.TopLeft:
                        Width = startWidth - deltaX;
                        Left = startLeft + deltaX;
                        Height = startHeight - deltaY;
                        Top = startTop + deltaY;
                        break;
                    case ResizeDirection.TopRight:
                        Width = startWidth + deltaX;
                        Height = startHeight - deltaY;
                        Top = startTop + deltaY;
                        break;
                    case ResizeDirection.BottomLeft:
                        Width = startWidth - deltaX;
                        Left = startLeft + deltaX;
                        Height = startHeight + deltaY;
                        break;
                    case ResizeDirection.BottomRight:
                        Width = startWidth + deltaX;
                        Height = startHeight + deltaY;
                        break;
                }
                startPoint.X = currentPoint.X;
                startPoint.Y = currentPoint.Y;
                startWidth = Width;
            }
        }

        // 鼠标释放事件处理
        private void ResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isResizing = false;
        }

        // 开始调整大小
        private void StartResizing(MouseButtonEventArgs e, ResizeDirection direction)
        {
            isResizing = true;
            startPoint = e.GetPosition(null);
            startWidth = Width;
            startHeight = Height;
            startLeft = Left;
            startTop = Top;
            CaptureMouse();
        }

        // 左侧调整区域鼠标移动事件处理
        private void LeftResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.Left);
        }

        // 右侧调整区域鼠标移动事件处理
        private void RightResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.Right);
        }

        // 顶部调整区域鼠标移动事件处理
        private void TopResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.Top);
        }

        // 底部调整区域鼠标移动事件处理
        private void BottomResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.Bottom);
        }

        // 左上角调整区域鼠标移动事件处理
        private void TopLeftResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.TopLeft);
        }

        // 右上角调整区域鼠标移动事件处理
        private void TopRightResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.TopRight);
        }

        // 左下角调整区域鼠标移动事件处理
        private void BottomLeftResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.BottomLeft);
        }

        // 右下角调整区域鼠标移动事件处理
        private void BottomRightResizeBorder_MouseMove(object sender, MouseEventArgs e)
        {
            ResizeBorder_MouseMove(sender, e, ResizeDirection.BottomRight);
        }

        // 左侧调整区域鼠标释放事件处理
        private void LeftResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 右侧调整区域鼠标释放事件处理
        private void RightResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 顶部调整区域鼠标释放事件处理
        private void TopResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 底部调整区域鼠标释放事件处理
        private void BottomResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 左上角调整区域鼠标释放事件处理
        private void TopLeftResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 右上角调整区域鼠标释放事件处理
        private void TopRightResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 左下角调整区域鼠标释放事件处理
        private void BottomLeftResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 右下角调整区域鼠标释放事件处理
        private void BottomRightResizeBorder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ResizeBorder_MouseLeftButtonUp(sender, e);
            ReleaseMouseCapture();
        }

        // 调整方向枚举
        private enum ResizeDirection
        {
            Left,
            Right,
            Top,
            Bottom,
            TopLeft,
            TopRight,
            BottomLeft,
            BottomRight
        }

        private void LeftResizeBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                LeftResizeBorder_MouseLeftButtonDown(sender, e);
                courRls++;
            }
        }

        private void LeftResizeBorder_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                LeftResizeBorder_MouseLeftButtonUp(sender, e);
                courRls++;
            }
        }
        int countPre = 0;
        private void RightResizeBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
               RightResizeBorder_MouseLeftButtonDown(sender, e);
                countPre++;
            }
        }
        int courRls = 0;
        private void RightResizeBorder_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
               RightResizeBorder_MouseLeftButtonUp(sender, e);
                courRls++;
            }
        }
        #endregion
    }
}